package org.adorsys.aderp.aderplogin.domain;

import javax.persistence.PrePersist;

import org.apache.commons.lang3.StringUtils;
import org.jasypt.util.password.StrongPasswordEncryptor;

public class PasswordEncryptionListener {

	private static StrongPasswordEncryptor jasyptPasswordEncryptor = new StrongPasswordEncryptor();

	@PrePersist
	public void encryptPassword(AderpUser aderpUser){
		// new entity, encrypt set passwords.
		if(StringUtils.isNotBlank(aderpUser.getPassword())){
			String np = jasyptPasswordEncryptor.encryptPassword(aderpUser.getPassword());
			aderpUser.setPassword(np);
		} 
	}
	
	public static boolean checkPassword(String plainPassword, String encryptedPassword){
		return jasyptPasswordEncryptor.checkPassword(plainPassword, encryptedPassword);
	}
	
	public static String encryptPassword(String plainPassword){
		return jasyptPasswordEncryptor.encryptPassword(plainPassword);
	}
}
